﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Photography.App_Data;
using System.Web.Configuration;
using System.IO;

namespace Photography.Admin
{
    public partial class Photo : System.Web.UI.Page
    {
        protected new Photography.Admin.AdminMaster Master { get { return base.Master as Photography.Admin.AdminMaster; } }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Master.AdminID.HasValue)
                Response.Redirect("Default.aspx");

            if (!IsPostBack && Master.PhotoId.HasValue)
            {
                btnSave.Text = "Save Photo";

                Photography.App_Data.PhotographyDataContext context = new Photography.App_Data.PhotographyDataContext();

                if (Master.GalleryId.HasValue)
                {
                    GetGalleryPhotosResult photo = (from a in context.GetGalleryPhotos(Master.GalleryId) where a.ID == Master.PhotoId select a).First();
                    txtTitle.Text = photo.Title;
                    txtDescription.Text = photo.Description;
                    txtLocation.Text = photo.Location;
                }
                else
                    throw new ApplicationException("Category not specified");
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Photography.App_Data.PhotographyDataContext context = new Photography.App_Data.PhotographyDataContext();

            if (!Master.PhotoId.HasValue)
            {
                if (Master.GalleryId.HasValue)
                    context.AddPhoto(txtTitle.Text, txtDescription.Text, txtLocation.Text, Master.GalleryId, chkSlideShow.Checked);
                else
                    throw new ApplicationException("Category not specified");
            }
            else
            {
                context.UpdatePhoto(Master.PhotoId, txtTitle.Text, txtDescription.Text, null, null, null, null, null);
            }
            Master.RedirectToManageGalleries();
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            if (fileNewPhoto.HasFile)
            {
                string path = MapPath(WebConfigurationManager.AppSettings["photosLocation"]);
                string filename = Path.GetFileNameWithoutExtension(fileNewPhoto.FileName);
                string ext = Path.GetExtension(fileNewPhoto.FileName);

                string target = "";

                for (int i = 0; i < int.MaxValue; i++)
                {
                    target = Path.Combine(path, filename + (i > 0 ? "_" + i.ToString() : "") + ext);
                    if (!File.Exists(target))
                        break;
                }

                if (!string.IsNullOrWhiteSpace(target))
                {
                    Photography.App_Data.PhotographyDataContext context = new Photography.App_Data.PhotographyDataContext();

                    fileNewPhoto.SaveAs(target);

                    string url = ResolveUrl(Path.Combine(WebConfigurationManager.AppSettings["photosLocation"], Path.GetFileName(target)));

                    if (Master.GalleryId.HasValue)
                        context.AddPhoto(txtTitle.Text, txtDescription.Text, url, Master.GalleryId, chkSlideShow.Checked);
                    else
                        throw new ApplicationException("Category not specified");
                }
                Master.RedirectToManageGalleries();
            }
        }
    }
}
